/**
 * 
 */
package kr.co.insoft.core.repository.configration.sev.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.insoft.core.mapper.configration.sev.SEVMapper;
import kr.co.insoft.core.mapper.configration.sev.SEVMetadataEntityMapper;
import kr.co.insoft.core.model.configration.sev.SEVEntityModel;
import kr.co.insoft.core.model.configration.sev.SevMappingEntityModel;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.repository.configration.sev.SEVRepository;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Repository
public class SEVRepositoryImpl implements SEVRepository {

	private static final Logger SEVRepositoryImplLog = LoggerFactory
			.getLogger(SEVRepositoryImpl.class);

	@Autowired
	SEVMapper sevMapper;

	@Autowired
	SEVMetadataEntityMapper sevMetadataEntityMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.variable.VariableRepository#getLists(kr.
	 * co.insoft.core.model.paging.PagingModel)
	 */
	@Override
	public List<SEVEntityModel> getLists(PagingModel pagingModel)
			throws RepositoryException {
		try {
			Assert.notNull(pagingModel);
			Assert.notNull(pagingModel.getCurrentPage());
		} catch (IllegalArgumentException e) {
			SEVRepositoryImplLog
					.error(ExceptionCode.ASSERT_FORMAT, pagingModel);
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					pagingModel.getCurrentPage());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return sevMapper.getLists(pagingModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.variable.VariableRepository#getTotalCount
	 * (kr.co.insoft.core.model.paging.PagingModel)
	 */
	@Override
	public int getTotalCount(PagingModel pagingModel)
			throws RepositoryException {
		try {
			Assert.notNull(pagingModel);
		} catch (IllegalArgumentException e) {
			SEVRepositoryImplLog
					.error(ExceptionCode.ASSERT_FORMAT, pagingModel);
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return sevMapper.getTotalCount(pagingModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.variable.VariableRepository#getVariable(
	 * kr.co.insoft.core.model.variable.VariableEntityModel)
	 */

	@Override
	public SEVEntityModel getSEV(SEVEntityModel sevEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(sevEntityModel);
			Assert.notNull(sevEntityModel.getVariableid());
		} catch (IllegalArgumentException e) {
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					sevEntityModel);
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					sevEntityModel.getVariableid());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return sevMapper.getSEV(sevEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.variable.VariableRepository#createVariable
	 * (kr.co.insoft.core.model.variable.VariableEntityModel)
	 */
	@Override
	public SEVEntityModel createSEV(SEVEntityModel sevEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(sevEntityModel);
			Assert.notNull(sevEntityModel.getVariableName());
			Assert.notNull(sevEntityModel.getUserid());
		} catch (IllegalArgumentException e) {
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					sevEntityModel);
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					sevEntityModel.getVariableName());
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					sevEntityModel.getUserid());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			sevEntityModel.setVariableid(sevMapper.getUuid());
			sevMapper.createSEV(sevEntityModel);
			return sevEntityModel;
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see kr.co.insoft.core.repository.setting.variable.VariableRepository#
	 * doEditVariable
	 * (kr.co.insoft.core.model.setting.variable.VariableEntityModel)
	 */
	@Override
	public void editSEV(SEVEntityModel sevEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(sevEntityModel);
			Assert.notNull(sevEntityModel.getVariableName());
			Assert.notNull(sevEntityModel.getVariableid());
			Assert.notNull(sevEntityModel.getUserid());
		} catch (IllegalArgumentException e) {
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					sevEntityModel);
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					sevEntityModel.getVariableName());
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					sevEntityModel.getVariableid());
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					sevEntityModel.getUserid());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			sevMapper.editSEV(sevEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see kr.co.insoft.core.repository.setting.variable.VariableRepository#
	 * doRemoveVariable
	 * (kr.co.insoft.core.model.setting.variable.VariableEntityModel)
	 */
	@Override
	public void removeSEV(SEVEntityModel sevEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(sevEntityModel);
			Assert.notNull(sevEntityModel.getVariableid());
		} catch (IllegalArgumentException e) {
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					sevEntityModel);
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					sevEntityModel.getVariableid());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			sevMapper.removeSEV(sevEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public void createSEVMapping(List<SevMappingEntityModel> mappings)
			throws RepositoryException {
		try {
			Assert.notEmpty(mappings);
		} catch (IllegalArgumentException e) {
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, mappings);
			return;
		}

		try {
			Map<String, List<SevMappingEntityModel>> param = new HashMap<String, List<SevMappingEntityModel>>();
			param.put("mappings", mappings);
			sevMapper.createSEVMapping(param);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public void removeSEVMapping(String orchid) throws RepositoryException {
		try {
			Assert.notNull(orchid);
		} catch (IllegalArgumentException e) {
			SEVRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, orchid);
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}
		try {
			sevMapper.removeSEVMapping(orchid);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
}
